#include "internal.h"
#include "PresenceSensor.h"
#include "PresenceSensor.tmh"

const PROPERTYKEY g_SupportedPIRProperties[] =
{
    SENSOR_PROPERTY_TYPE,
    SENSOR_PROPERTY_STATE,
    SENSOR_PROPERTY_CURRENT_REPORT_INTERVAL,
    SENSOR_PROPERTY_PERSISTENT_UNIQUE_ID,
    SENSOR_PROPERTY_FRIENDLY_NAME,
    SENSOR_PROPERTY_DESCRIPTION, 
    SENSOR_PROPERTY_CONNECTION_TYPE,
    WPD_FUNCTIONAL_OBJECT_CATEGORY,
};

const PROPERTYKEY g_SupportedPIRDataFields[] = 
{
    SENSOR_DATA_TYPE_TIMESTAMP,
    SENSOR_DATA_TYPE_HUMAN_PRESENCE,
};

PresenceSensor::PresenceSensor(WpdObjectProperties* pWpdObjectProperties): 
	SensorBase(pWpdObjectProperties) 
{
	m_strSensorName		= SENSOR_PIR_FRIENDLY_NAME;
	m_strSensorId		= SENSOR_PIR_ID;
	m_gdSensorUniqueId	= SENSOR_PIR_GUID;
	m_gdSensorCategory	= SENSOR_CATEGORY_BIOMETRIC;
	m_gdSensorType		= SENSOR_TYPE_HUMAN_PRESENCE;
}

VOID PresenceSensor::AddPropertyKeys(
	IPortableDeviceKeyCollection* pKeys)
{
    if (pKeys != NULL)
    {
        for (DWORD dwIndex = 0; dwIndex < ARRAYSIZE(g_SupportedPIRProperties); dwIndex++)
        {
            pKeys->Add(g_SupportedPIRProperties[dwIndex] );
        }
    }
}

VOID PresenceSensor::AddDataFieldKeys(
	IPortableDeviceKeyCollection* pKeys)
{
    if (pKeys != NULL)
    {
        for (DWORD dwIndex = 0; dwIndex < ARRAYSIZE(g_SupportedPIRDataFields); dwIndex++)
        {
            pKeys->Add(g_SupportedPIRDataFields[dwIndex] );
        }
    }
}

HRESULT PresenceSensor::OnGetDataValues(
    PROPERTYKEY &Key,
    IPortableDeviceValues*         pValues)
{
    HRESULT hr		= S_OK;

	// Set PIR sensor data   
	if (IsEqualPropertyKey(Key, SENSOR_DATA_TYPE_HUMAN_PRESENCE))
	{
        PROPVARIANT var;
        PropVariantInit( &var );
        var.vt = VT_BOOL;

		if (m_llSensorReading == 5110)
			 var.boolVal = false;
		else if (m_llSensorReading == 5111)
			var.boolVal = true;
	
		hr = pValues->SetValue(SENSOR_DATA_TYPE_HUMAN_PRESENCE, &var);
		
		// Free any allocated resources
		PropVariantClear( &var );
	}

	return hr;
}